package com.mxpio.erp.equipment.service.impl;

import com.mxpio.erp.common.equipment.entity.EquipmentRepairUrgeLine;
import com.mxpio.erp.common.equipment.service.EquipmentRepairService;
import com.mxpio.erp.common.equipment.service.EquipmentRepairUrgeLineService;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 设备维修-催办记录
 * @Author: jeecg-boot
 * @Date:   2021-09-23
 * @Version: V1.0
 */
@Service
@Slf4j
public class EquipmentRepairUrgeLineServiceImpl extends BaseServiceImpl<EquipmentRepairUrgeLine> implements EquipmentRepairUrgeLineService {

	@Resource
	private EquipmentRepairService equipmentRepairService;
	/*@Resource
	private MessageService messageService;
	@Value("${dingtalk.executeDetailUrl}")
	private String executeDetailUrl;*/

	public static final String[] channelCodes = {"dingtalkWorkMsg"};
	@Override
	public List<EquipmentRepairUrgeLine> selectByMainId(String mainId) {
		Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, mainId);
		return list(EquipmentRepairUrgeLine.class,c);
	}

	@Override
	@Transactional
	public void saveAndSendMessage(EquipmentRepairUrgeLine equipmentUpkeepTaskDetail, boolean isSend) {
		if (isSend){
			/*String orderCode = equipmentUpkeepTaskDetail.getOrderCode();
			EquipmentRepair equipmentRepair = equipmentRepairService.getByOrderCode(orderCode);
			if (equipmentRepair!=null){
				//发送消息
				User dbUser = JpaUtil.getOne(User.class, equipmentRepair.getDistributePersonId());
				if (dbUser!=null&& StringUtils.isNotBlank(dbUser.getThirdId())){
					DingTalkMessagePojo dingTalkMessagePojo = new DingTalkMessagePojo();
					dingTalkMessagePojo.setMsgtype(DingTalkEnums.MsgType.LINK.getCode());
					LinkTypeContent linkTypeContent = new LinkTypeContent();
					linkTypeContent.setMessageUrl(executeDetailUrl+equipmentRepair.getOrderCode());
					linkTypeContent.setPicUrl("@lADOADmaWMzazQKA");
					String title = "维修催办";
					linkTypeContent.setTitle(title);
					linkTypeContent.setText(DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss")+"维修任务催办，请及时处理，单号："+equipmentRepair.getOrderCode());
					dingTalkMessagePojo.setLink(linkTypeContent);
					//log.error("thirdUserIdsStr:"+dbUser.getThirdId()+";"+JSONObject.toJSONString(dingTalkMessagePojo));
					messageService.sendMessage(channelCodes,"",dbUser.getThirdId().split(","),title, JSONObject.toJSONString(dingTalkMessagePojo));
				}
			}*/
		}
		this.save(equipmentUpkeepTaskDetail);
	}
}
